//
//  AppDelegate.m
//  BowJoy
//
//  Created by  on 2024/5/13.
//

#import "AppDelegate.h"
#import "BJLoginViewController.h"
#import "BJTabBarViewController.h"
#import "BJLaunchViewController.h"
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import "BJGuidePageView.h"

@interface AppDelegate ()
@property (nonatomic) BJLaunchViewController *launchVc;
@property (nonatomic) BJLoginViewController *loginVc;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
   BOOL isFristOpen = [[NSUserDefaults standardUserDefaults] objectForKey:@"isFristOpen"];
    [BJNetManager shareNetManager].serialId = 0;
    [BJNetManager shareNetManager].launcherId = [NSString stringWithFormat:@"%@$%lld",[UUIDStrengthen getDeviceIDInKeychain],[self gs_getCurrentTimeToMilliSecond]];
    
    if (isFristOpen) {
        [self getLoginData];
    } else {
        self.window.rootViewController = self.launchVc;
    }
    
    return YES;
}
- (long long)gs_getCurrentTimeToMilliSecond {
    
    double currentTime = [[NSDate date] timeIntervalSince1970] * 1000;
    
    long long iTime = (long long)currentTime;
    
    return iTime;
    
}

//获取是否登录
-(void)getLoginData{
    kWeakSelf(weakSelf)
    [[BJNetManager shareNetManager] postIsValidTokenWithCompletionHandler:^(id  _Nonnull responOj, NSError * _Nonnull error) {
        BOOL isLogin = false;
        if(responOj[@"data"]){
            isLogin = [responOj[@"data"] boolValue];
        }
        [weakSelf getGetStrategyDataIsLogin:isLogin];
    }];
}

//获取策略数据
-(void)getGetStrategyDataIsLogin:(BOOL)isLogin{
    if(isLogin){
        //获取策略数据的前提是必须登录了
        kWeakSelf(weakSelf)
        [[BJNetManager shareNetManager] getGetStrategyWithCompletionHandler:^(id   responOj, NSError * _Nonnull error) {
            [weakSelf.window setRootViewController:[[BJTabBarViewController alloc]init]];
        }];
    }else{
        [self.window setRootViewController:self.loginVc];

    }
}




- (BJLaunchViewController *)launchVc{
    kWeakSelf(weakSelf)
    AsGetMethodReturnObjc(_launchVc);
    _launchVc = [[BJLaunchViewController alloc] init];
    [_launchVc setLoadDataCallBack:^{
        [weakSelf.window setRootViewController:weakSelf.loginVc];
    }];
    return _launchVc;
}
- (BJLoginViewController *)loginVc{
    kWeakSelf(weakSelf)
    AsGetMethodReturnObjc(_loginVc);
    _loginVc = [[BJLoginViewController alloc] init];
    [_loginVc setLoginSuccessCallBack:^(BOOL isFirstRegister) {
        //获取策略
        [[BJNetManager shareNetManager] getGetStrategyWithCompletionHandler:^(id   responOj, NSError * _Nonnull error) {
            [weakSelf.window setRootViewController:[[BJTabBarViewController alloc]init]];
        }];
    }];
    return _loginVc;
}

-(void)applicationDidBecomeActive:(UIApplication *)application{
    
    if (@available(iOS 14.0, *)) {
        // iOS14及以上版本请求权限
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            BOOL authorized = status == ATTrackingManagerAuthorizationStatusAuthorized;
            
        }];
    } else {
    }

 
}
- (void)setRootViewControllerForLoginVc{
    [self.window setRootViewController:self.loginVc];
}

- (UIWindow *)window{
    AsGetMethodReturnObjc(_window);
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor = [UIColor whiteColor];
    [_window makeKeyAndVisible];
    return _window;
}

@end
